package com.tang.service.impl;

import com.tang.service.MediaPlayer;
import com.tang.service.adapter.MediaAdapter;
import com.tang.service.utils.MediaUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 播放器类
 *
 * @author fulin.tang
 * @version V3.0
 * @since 2021/9/16 14:05
 */
@Slf4j
public class AudioPlayer implements MediaPlayer {

    private static final String MP3 = MediaUtil.getInstance().MP3;

    MediaAdapter mediaAdapter;

    @Override
    public void play(String audioType, String fileName) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        if (MP3.equalsIgnoreCase(audioType)) {
            log.info("Play {} file.name: {}", MP3, fileName);
        } else if (MediaUtil.getInstance().containsAudioType(audioType)) {
            this.mediaAdapter = MediaUtil.getInstance().getMediaAdapter(audioType);
            if (this.mediaAdapter == null) {
                this.mediaAdapter = new MediaAdapter(audioType);
                MediaUtil.getInstance().setMediaAdapter(audioType, this.mediaAdapter);
            }
            this.mediaAdapter.play(audioType, fileName);
        } else {
            log.error("Not support this audioType!");
        }
    }

}
